home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2625 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  81 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Multiple Abstract Classes Question
  5. Date: 18 Jan 1996 19:48:13 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan18204813@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <1996Jan17.160425.6063@ned.cray.com>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: aztec@cray.com's message of 17 Jan 96 16:04:25 CST
  12.  
  13. In article <1996Jan17.160425.6063@ned.cray.com> aztec@cray.com (Joel Garcia-Trevino  {x66457 CF/DEV}) writes:
  14.  
  15.     Can I or is it a good idea to have multiple abstract classes to start building
  16.    a class hierarchy?
  17.  
  18.      Example of the base (abstract) classes:
  19.  
  20.       class base_type; 
  21.  
  22.       class base_net {
  23.        public:
  24.         virtual char         *get_name() = 0;    // Gets the name
  25.         virtual base_type    *get_type() = 0;    // Gets pointer to type
  26.       };
  27.  
  28.       class base_type {
  29.        public:
  30.         virtual char *get_type() = 0;            // Gets type name 
  31.       };
  32.  
  33.  
  34.      The final classes would look something like:
  35.  
  36.      class type;
  37.  
  38.      class net: public base_net {
  39.        private:
  40.         char *name;
  41.         type *type_pntr;
  42.  
  43.        public:
  44.         char *get_name() { ..... };
  45.         type *get_type() { ..... }; 
  46.  
  47.      };
  48.  
  49.     class type: public base_type {
  50.       private:
  51.        char *type_name;
  52.  
  53.       public:
  54.        char *get_type() { ..... };
  55.  
  56.     };
  57.  
  58.  
  59.     Does it make sense to do it this way?
  60.  
  61.     The reason why I want to do this is because there will be different types
  62.    of nets that I will need to work with. I don't know if there will be
  63.    different types of "type" but I thought why not make them all abstract. ???
  64.  
  65. In theory building parallel class-hierachies is nothing bad.
  66. However things become it bit uncomfortable if your compiler
  67. doesn't support covariant return types. In this case you can
  68. not have different return types for different implementations
  69. of a polymorphic function. Therefore '{net,bas_net} :: get_type'
  70. would both have to return a 'base_type' pointer which has to
  71. be narrowed using the old-style cast or the 'dynamic_cast'
  72. (or an appropriate 'emulation' of it). So your approach seems
  73. reasonable but can cause slightly problems with todays compilers.
  74. Anyway you should also take a solution using template specialization
  75. into consideration.
  76.  
  77.         Enno
  78.  
  79.  
  80.  
  81.